<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/23
 * Time: 15:19
 */

namespace app\logic\index\lists;


use app\logic\BaseLogic;
use app\model\huitun\HuitunTalentDataModel;
use app\model\huitun\HuitunVideoDataModel;
use support\Db;

class ListsLogic extends BaseLogic
{
    protected static $instance;

    public function __construct()
    {
        parent::__construct();
    }

    public static function getInstance()
    {
//        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return  new self();
    }

    public function getTalentList($param)
    {
        $createdAt = HuitunTalentDataModel::getInstance()->max('created_at');
        $createdAt && $createdAt = date('Y-m-d 00:00:00',strtotime($createdAt));
        $where = [['id','>',0],['created_at','>=',$createdAt]];
        $fields = ['authorid','nickname','avatarurl',Db::Raw('convert(followercounttotal,decimal) as followercounttotal')
                   ,Db::Raw('convert(incfans,decimal) as incfans'),Db::Raw('convert(diggavg,decimal) as diggavg'),'created_at'];

        $order = ['followercounttotal', 'desc'];
        if(!empty($param['sortfield']) && in_array($param['sortfield'],['followercounttotal','incfans','diggavg'])){
            $order[0] = $param['sortfield'];
        }
        $order[1] = $param['sortmod']?:'desc';

        $list =  HuitunTalentDataModel::getInstance()->index($where, $fields, $order);

        foreach($list['data'] as $k=>$val){
            $val['followercounttotal'] = (string) $this->formatNumber($val['followercounttotal']);
            $val['incfans'] = (string) $this->formatNumber($val['incfans']);
            $val['diggavg'] = (string) $this->formatNumber($val['diggavg']);
        }

        return $list;
    }

    public function getVideoList($param)
    {
        $createdAt = HuitunVideoDataModel::getInstance()->max('created_at');
        $createdAt && $createdAt = date('Y-m-d 00:00:00',strtotime($createdAt));
        $where = [['id','>',0],['created_at','>=',$createdAt]];
        $fields = ['desc','coveruri',Db::Raw('convert(commentcount,decimal) as commentcount')
                   ,Db::Raw('convert(diggcount,decimal) as diggcount'),'created_at'];

        $order = ['diggcount', 'desc'];
        if(!empty($param['sortfield']) && in_array($param['sortfield'],['commentcount','diggcount'])){
            $order[0] = $param['sortfield'];
        }
        $order[1] = $param['sortmod']?:'desc';

        $list =  HuitunVideoDataModel::getInstance()->index($where, $fields, $order);

        foreach($list['data'] as $k=>$val){
            $val['commentcount'] = (string) $this->formatNumber($val['commentcount']);
            $val['diggcount'] = (string) $this->formatNumber($val['diggcount']);
        }

        return $list;
    }

    protected function formatNumber($number)
    {
        if($number>=10000){
            $numbers = explode('.',(string)(round($number/10000,1)));
            $numberStr = [];
            $numberStr[0] = number_format($numbers[0],0,'.',',');
            $numberStr[1] = !empty($numbers[1]) ? '.'.$numbers[1] :'';

            return $numberStr[0].$numberStr[1].'W';
        }else{
            $numbers = explode('.',(string)$number);
            $numberStr = [];
            $numberStr[0] = number_format($numbers[0],0,'.',',');
            $numberStr[1] = !empty($numbers[1]) ? '.'.$numbers[1] :'';

            return $numberStr[0].$numberStr[1];
        }
    }
}